﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Phoenix.Service.Contracts;
using Phoenix.Service;
using Phoenix.Service.Model;
using System.Collections;

namespace Phoenix.AdminSite.Controllers
{
    public class DepartmentController : Controller
    {
        static IDepartmentService departmentService = new DepartmentService();
		IIDCreaterService IIDCreater = new IDCreaterService();

        /// <summary>
        /// 取所有部门
        /// </summary>
        public IList<DepartmentModel> departments
        {
            get
            {
                var departments = departmentService.GetAll();
                if (departments == null)
                {
                    departments = new List<DepartmentModel>();
                }
                return departments;
            }
        }


        public ActionResult Index()
        {
            return View();
        }

    
        // 根据父类取子类组织架构
        public ActionResult ChildDepartmentList(int departmentID)
        {
            var departmentsTmp = departments.Where(p => p.ParentID == departmentID && !p.IsClosed).ToList();

            return Json(departmentsTmp, JsonRequestBehavior.AllowGet);
        }

        // 通过父类ID取子类
        public JsonResult GetChildDepartment()
        {
            return GetParentDepartmentCommon(0);
        }


		public ActionResult Organization(int? departmentID)
		{
			ViewData["DepartmentID"] = departmentID;
			return View();
		}



		public ActionResult Edit(string DepartmentID)
        {
			if (string.IsNullOrEmpty(DepartmentID)) return View();

			DepartmentModel model = departmentService.GetByDepartmentID(int.Parse(DepartmentID));

			ViewData["ParentName"] = GetDepartmentName(model.ParentID);

			ViewData["Department"] = model;

            return View();
        }

		public ActionResult Save(DepartmentModel model)
		{
			if (model.DepartmentID > 0)
			{
				DepartmentModel oldModel = departmentService.GetByDepartmentID(model.DepartmentID);
				model.CreateTime = oldModel.CreateTime;
				model.IsClosed = oldModel.IsClosed;
				model.Level = oldModel.Level;
				//model.ParentID = oldModel.ParentID;
				departmentService.Update(model);
               
			}
			else
			{
				model.DepartmentID = IIDCreater.GetNewID("DepartmentID", "部门编号", 1000);
				model.CreateTime = DateTime.Now;
				model.IsClosed = false;
				model.ParentID = 0;
			
			}

            return Content(CommService.JsAlertAndParentPara("保存成功!", "/department/index?departmentID=" + model.ParentID));
			
		}

        #region 私有方法

        /// <summary>
        /// 公共方法,渠道部和所有TreeView数据
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        private JsonResult GetParentDepartmentCommon(int type)
        {
            ArrayList array = new ArrayList();

            // 取全部菜单
            IList<DepartmentModel> departments = departmentService.GetAll();

            if (departments == null) return null;

            // 取父类菜单 渠道的Level(级别): 0-1-1-2-3-
            var parentDepartments = type == 0 ? departments.Where(p => p.ParentID == 0) :
                departments.Where(p => p.Level.IndexOf("0-1-1-2-3-") > -1);
            foreach (DepartmentModel departmentModel in parentDepartments)
            {
                // 父类TreeView数据
                object objParent = new
                {
                    id = departmentModel.DepartmentID,
                    text = departmentModel.Name,
                    attr = new { value = departmentModel.DepartmentID, name = "chkMenuID", nodeidDB = departmentModel.Level }
                };
                array.Add(objParent);

                // 递归取子菜单
                array.AddRange(GetChildDepartment(departments, departmentModel.DepartmentID));
            }

            return Json(array, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 递归取子菜单
        /// </summary>
        /// <param name="functions">所有菜单</param>
        /// <param name="functionID">ID</param>
        /// <returns></returns>
        private ArrayList GetChildDepartment(IList<DepartmentModel> departments, int departmentID)
        {
            ArrayList array = new ArrayList();
            var childMenus = departments.Where(p => p.ParentID == departmentID);

            foreach (DepartmentModel menuChild in childMenus)
            {
                // 通过父类ID找子菜单,添加到arrayList里,供TreeView使用
                object objChild = new
                {
                    id = menuChild.DepartmentID,
                    pid = departmentID,
                    text = menuChild.Name,
                    attr = new { value = menuChild.DepartmentID, name = "chkMenuID", nodeidDB = menuChild.Level }
                };

                array.Add(objChild);

                var childs = GetChildDepartment(departments, menuChild.DepartmentID);

                if (childs.Count > 0)
                {
                    array.AddRange(childs);
                }
            }

            return array;
        }


		/// <summary>
		/// 获取部门的名称
		/// </summary>
		/// <param name="departmentID">部门ID</param>
		/// <returns>部门名称</returns>
		private string GetDepartmentName(int departmentID)
		{
			var department = departments.Where(p => p.DepartmentID == departmentID).FirstOrDefault();
			return department != null ? department.Name : string.Empty;
		}


        #endregion

    }
}
